#pragma once
#include <memory>
#include <functional>
#include <queue>
#include <QBuffer>
#include "comLib\VideoDecoder.h"
#include "comLib\NetPacket.h"
#include <common\MsgDef.h>
#include <HPSocket\HPSocket.h>
#include <common\singletonPattern.h>
#include <common\def.h>
#include <QEvent>

class CNet;
class DMsgCenter;

class DDataProcess :public CTcpClientListener
{
public:
	DDataProcess(QObject *parent = nullptr);
	~DDataProcess();

protected:
	EnHandleResult OnHandShake(ITcpClient* pSender, CONNID dwConnID) override;
	EnHandleResult OnReceive(ITcpClient* pSender, CONNID dwConnID, const BYTE* pData, int iLength)	 override;
	EnHandleResult OnSend(ITcpClient* pSender, CONNID dwConnID, const BYTE* pData, int iLength)	 override;
	EnHandleResult OnClose(ITcpClient* pSender, CONNID dwConnID, EnSocketOperation enOperation, int iErrorCode) override;

	void run(const HeadInfo &headInfo, const void *data);

private:
	struct PacketStruct{
		bool bCompleteHead;
		HeadInfo headInfo;
		QByteArray *buffer;

		PacketStruct(){
			bCompleteHead = false;
			buffer = nullptr;
		}
	};

private:
	bool ProcessMouseInfo(const HeadInfo, const void *);

private:
	typedef std::map <SERVER_MSG,std::function<bool(const HeadInfo , const void *)>> MsgProcess;

	int m_iHeadSize;
	std::queue<NetPacket> m_PacketQueue;
	PacketStruct m_RecvPacket;
	std::map<Qt::MouseButton, int> m_mousePressMap, m_mouseReleaseMap;

	MsgProcess m_MsgProcess;
	DMsgCenter	*m_pMsgCenter;
};

class DNetClient :public SingleTon<DNetClient>
{
public:
	~DNetClient();
	bool connectServer();
	ITcpClient *getTcpClient(){ return m_pTcpClient; }
	void sendPacket(HeadInfo headInfo, const char *data);

private:
	DNetClient();

private:
	friend class SingleTon < DNetClient >;
	std::shared_ptr<DDataProcess> m_pDataProcess;

	ITcpClient *m_pTcpClient;
	QString m_BindIp;
};
